package com.chart.web.controller.chart;

import com.chart.chart.domian.req.VGenerateReq;

import com.chart.chart.domian.req.VerifiedReq;
import com.chart.chart.service.IChartVouchersService;
import com.chart.common.core.domain.AjaxResult;

import com.chart.common.core.domain.model.LoginUser;
import com.chart.common.utils.SecurityUtils;
import com.chart.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "兑换码模块")
@RestController
@RequestMapping("/vouchers")
public class ChartVouchersController {

    @Autowired
    private IChartVouchersService chartVouchersService;

    @Resource
    private TokenService tokenService;

//    @ApiOperation("兑换码生成")
//    @PostMapping("/generate")
//    public AjaxResult vouchersGenerate(@RequestBody VGenerateReq req) {
//        String generate = chartVouchersService.generate(req.getType());
//
//        Integer type = req.getType();
//        String name;
//        switch (type){
//            case 1:
//                name = "一个月会员";
//                break;
//            case 2:
//                name = "三个月会员";
//                break;
//            case 3:
//                name = "12个月会员";
//                break;
//        }
//
//
//        HashMap<Object, Object> map = new HashMap<>();
//        map.put("generate", generate);
//        map.put("name",name);
//        return  AjaxResult.success(map);
//    }

    @ApiOperation("兑换码生成")
    @PostMapping("/generate")
    public AjaxResult vouchersGenerate(@RequestBody VGenerateReq req) {
        String generate = chartVouchersService.generate(req.getType());
        Integer type = req.getType();
        String name;
        // 使用 switch 语句根据类型设置名称
        switch (type) {
            case 1:
                name = "一个月会员";
                break;
            case 2:
                name = "三个月会员";
                break;
            case 3:
                name = "一年会员";
                break;
            case 4:
                name = "一次性使用优惠券";
                break;
            default:
                name = "未知类型";
                break;
        }
        // 创建返回的Map
        Map<String, Object> map = new HashMap<>();
        map.put("generate", generate);
        map.put("name", name);
        return AjaxResult.success(map);
    }




    @ApiOperation("兑换码核销")
    @PostMapping("/verified")
    public AjaxResult verified(@RequestBody VerifiedReq req){
        String token = tokenService.getTokenKey1(SecurityUtils.getLoginUser().getToken());
        return chartVouchersService.verified(req,token);
    }

}
